package io.framework.modules.air.utils;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import io.framework.common.utils.Result;
import io.framework.modules.air.service.AirDayNumberService;
import io.framework.modules.air.service.AirService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 空气实时数据请求
 *
 * @author Gujq email
 * @since 1.0.0 2020-05-20
 */
public class AirMonitorUtil {

    public static Map<String, Object> getAirMinitorInfo(Map<String, Object> params) {
        HttpResponse r = HttpUtil.createGet(params.get("address").toString()).body("", "text/plain").charset("GBK").execute();
        if (!r.isOk()) {
            throw new RuntimeException("获取空气数据失败: status=" + r.getStatus());
        }
        Object v = r.body();
        if(v==null){
            throw new RuntimeException("暂无数据");
        }
        Map<String,Object> map = new HashMap<>();
        String data[] = v.toString().split(",");
        map.put("createDate",data[0]);
        map.put("stationName",data[1]);
        map.put("so2",data[2]);
        map.put("no2",data[3]);
        map.put("co",data[4]);
        map.put("o3",data[5]);
        map.put("pm10",data[6]);
        map.put("PM25",data[7]);
        map.put("aqi",data[8]);
        map.put("classify",data[9]);
        map.put("level",data[10]);
        map.put("top",data[11]);
        return map;
    }
}